package com.rlevin.java.chmod;

import junit.framework.TestCase;

public class UgoTest extends TestCase {

	private Ugo ugo;
	
	public UgoTest(String name) {
		super(name);
	}

	protected void setUp() throws Exception {
		super.setUp();
		// like chmod 755 ;)
		Chmodable user = new User(new Permissions(
				Permissions.Flag.READ, Permissions.Flag.WRITE, Permissions.Flag.EXECUTE));
		Chmodable group = new Group(new Permissions(
				Permissions.Flag.READ,Permissions.Flag.EXECUTE));
		Chmodable other = new Other(new Permissions(
				Permissions.Flag.READ,Permissions.Flag.EXECUTE));

		this.ugo = new Ugo(user,group,other);
	}

	protected void tearDown() throws Exception {
		super.tearDown();
	}
	
	public void testConstructor() throws Exception {
		//System.out.printf(ugo.toString());
		assertNotNull(ugo);
	}
	
	/**
	 * TODO: Are these 'brittle' because it depends on the setUp being chmod 755 ???
	 * @throws Exception
	 */
	public void testGetters() throws Exception {
		Chmodable user = ugo.getUser();
		assertEquals(User.class, user.getClass());
		assertEquals(Permissions.PermissionLevel.READ_WRITE_EXECUTE.toString(), 
				user.getPermissionsAsString());
		
		Chmodable group = ugo.getGroup();
		assertEquals(Group.class, group.getClass());
		assertEquals(Permissions.PermissionLevel.READ_EXECUTE.toString(), 
				group.getPermissionsAsString());
		
		Chmodable other = ugo.getOther();
		assertEquals(Other.class, other.getClass());
		assertEquals(Permissions.PermissionLevel.READ_EXECUTE.toString(), 
				other.getPermissionsAsString());
	}
	
	public void testGetPermissionsAsInt() throws Exception {
		assertTrue(755==ugo.getPermissionsAsInt());
	}

	public void testChmod() throws Exception {
		ugo.chmod("755");
		assertEquals(755,ugo.getPermissionsAsInt());
		ugo.chmod("750");
		assertEquals(750, ugo.getPermissionsAsInt());
	}
	
	public void testIllegalChmodTooHigh() throws Exception {
		try {
			ugo.chmod("888");
			fail("Should have caught IllegalArgumentException.");
		} catch (Exception e) {
			assertEquals(IllegalArgumentException.class, e.getClass());
		}
	}
	
	/**
	 * As of now, legal values are between 111-777 as I'll have to convert to octal to use leading zeros.
	 * @throws Exception
	 */
	public void testIllegalLeadingZeroArg() throws Exception {
		try {
			ugo.chmod("010");
			fail("Should have caught IllegalArgumentException.");
		} catch (Exception e) {
			assertEquals(IllegalArgumentException.class, e.getClass());
		}
	}
	
}
